﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.StarterSite.Controllers;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core;
using MvcEngine.Blog.Filters;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;
using MvcEngine.Blog.Managers;
using MvcEngine.Blog.Models;
using MvcEngine.Mvc.Extensions;

namespace MvcEngine.Blog.BackOffice.Controllers
{
    public class BlogCategoryController : BaseController
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel List(Guid blogId, int? pageNumber)
        {
            if (!pageNumber.HasValue || pageNumber <= 0)
                pageNumber = 1;

            IPagination<BlogCategory> pagination = BlogCategoryManager.Current.Get(blogId, pageNumber.Value, 10);

            var blog = BlogManager.Current.GetBlog(blogId);
            BlogCategoryListPaginationViewModel model = new BlogCategoryListPaginationViewModel()
            {
                TotalItems = pagination.TotalItems,
                TotalPages = pagination.TotalPages,
                BlogName = blog.Name,
                Categories = new List<BlogCategoryDetailsViewModel>(),
                ViewName = "List",
                IsCorrectPage = true
            };

            if (pageNumber.Value > pagination.TotalPages && pagination.TotalPages != 0)
            {
                model.IsCorrectPage = false;
            }

            if (pagination.TotalItems == 0)
                return model;

            foreach (var category in pagination)
            {
                var categoryViewModel = new BlogCategoryDetailsViewModel()
                {
                    Id = category.Id,
                    Slug = category.Slug,
                    BlogId = category.BlogId,
                    Name = category.Name,
                    CreatedDate = category.CreatedDate,
                    ModifiedDate = category.ModifiedDate,
                };
                model.Categories.Add(categoryViewModel);
            }
            return model;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel Edit(Guid blogId, Guid? id)
        {
            var model = new BlogCategoryDetailsViewModel()
            {
                ViewName = "Edit",
                BlogId = blogId
            };
            if (id.HasValue)
            {
                var category = BlogCategoryManager.Current.Get(id.Value);
                model.Name = category.Name;
                model.Slug = category.Slug;
                model.Id = category.Id;
            }
            return model;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [AntiForgeryValidationFilter]
        [ActionName("Edit")]
        public object Save(Guid blogId, Guid? id, BlogCategory category)
        {
            if (ModelState.IsValid)
            {
                if (id.HasValue)
                {
                    category.Id = id.Value;
                }
                category.BlogId = blogId;
                BlogCategoryManager.Current.Save(category);

                return Json(new { model = "valid" });
            }
            return Json(new { model = "invalid", errors = ModelState.GetErrors() });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [AntiForgeryValidationFilter]
        public object Delete(Guid id)
        {
            BlogCategoryManager.Current.Delete(id);
            return Redirect(Url.BlogCategories());
        }
    }
}
